package InterruptThread;

public class Method1 {
    public static boolean flg = true;
    //1.使用标志位来控制线程是否停止
    public static void main (String[] args) throws InterruptedException {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                while (flg) {
                    System.out.println("hello!!");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }

            }
        });
        t.start();
        //这个代码之所以能够起到让进程结束的作用,完全是取决于t线程内部的代码,代码中通过flg控制循环使得这个t线程是否停止执行
        Thread.sleep(3000);
        flg = false;
    }
}
